<template>
	<div>
		<el-row>
			<!-- 树形组件 -->
			<el-col :span="4">
				<div style="text-align:center">
					<el-tree :data="dataTree" highlight-current default-expand-all :props="defaultProps"
						@node-click="handleNodeClick">
					</el-tree>
				</div>
			</el-col>
			<!-- 表格组件 -->
			<el-col :span="20">
				<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
					label-width="100px">
					<el-form-item label="商品类型名称" prop="typeName">
						<el-input v-model="queryParams.typeName" placeholder="商品类型名称" clearable
							@keyup.enter.native="handleQuery" />
					</el-form-item>
					<el-button type="primary" icon="el-icon-search" size="mini" @click="quarryList()">搜索</el-button>
					<el-button icon="el-icon-refresh" size="mini" @click="reset()">重置</el-button>
				</el-form>
				<el-button type="success" icon="el-icon-plus" size="mini" @click="addFrom()">新增</el-button>
				<el-button type="danger" icon="el-icon-delete" size="mini" :disabled="delClick" @click="delMany()">删除
				</el-button>
				<el-table ref="multipleTable" :data="dataList" tooltip-effect="dark"
					@selection-change="handleSelectionChange">
					<el-table-column type="selection" width="55">
					</el-table-column>
					<el-table-column prop="id" label="序号" align="center">
					</el-table-column>
					<el-table-column prop="typeName" label="品牌类型名称" align="center">
					</el-table-column>
					<el-table-column prop="desc" label="品牌类型描述" align="center" show-overflow-tooltip>
					</el-table-column>
					<el-table-column prop="brand" label="操作" align="center" show-overflow-tooltip>
						<template slot-scope="scope">
							<el-button size="mini" @click="handleChoose(scope.$index,scope.row)">选用品牌</el-button>
							<el-button size="mini" type="success" @click="handleEdit(scope.$index,scope.row)">修改
							</el-button>
							<el-button size="mini" type="danger" @click="handleDelete(scope.$index,scope.row)">删除
							</el-button>
						</template>
					</el-table-column>
				</el-table>
				<!-- 分页组件 -->
				<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
					:current-page="queryParams.pageNum" :page-sizes="[3, 5, 8, 10]" :page-size="queryParams.pageSize"
					layout="->,total, sizes, prev, pager, next, jumper" :total="queryParams.total">
				</el-pagination>
			</el-col>
		</el-row>

		<!-- 添加或修改参数配置对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
			<el-form ref="dataForm" :model="dataForm" :rules="rules" label-width="100px">
				<el-form-item label="上级商品类型" prop="pid">
					<el-cascader ref="parentTypeCascader" v-model="dataForm.pid" :options="dataTree" :props="defaultProps2" placeholder="上级商品类型选择" @change="changePid"></el-cascader>
					
				</el-form-item>
				<el-form-item label="商品类型名称" prop="typeName">
					<el-input v-model="dataForm.typeName" placeholder="请输入商品类型名称" />
				</el-form-item>
				<el-form-item label="商品类型描述" prop="desc">
					<el-input v-model="dataForm.desc" type="textarea" placeholder="请输入内容"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>

		<!-- 选用品牌穿梭框 -->
		<el-dialog :title="titleBrand" :visible.sync="chooseBrand" width="625px" append-to-body>
			<el-transfer :titles="titlesBrand" v-model="valBrand" :data="dataBrand" :props="{ key: 'id',label: 'brandName'}">
			</el-transfer>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitBrand">确 定</el-button>
				<el-button @click="cancelBrand">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				queryParams: {
					pid: '',
					typeName: '',
					pageNum: 1,
					pageSize: 10,
					total: 0,
				},
				dataList: [],
				showSearch: true,
				open: false,
				chooseBrand: false,
				//表单校验
				rules: {},
				chooseAll: [],
				dataTree: [],
				defaultProps: {
					value:'id',
					children: 'children',
					label: 'typeName'
				},
				defaultProps2: {
					value:'id',
					children: 'children',
					label: 'typeName',
					checkStrictly: true 
				},
				title: '',
				dataForm: {
					pid: '',
					typeName: '',
					brandTypeDescription: '',
					id: '',
				},
				//所有的商品类型
				allList: [],
				//穿梭框数据
				dataBrand: [],
				valBrand: [],
				titleBrand: "选用品牌",
				titlesBrand: ['可选品牌', '已选品牌'],
				typeId: '',
				delClick: true,
			};
		},
		methods: {
			changePid(a){
				let pid= a.at(-1);
				this.dataForm.pid=pid;
			},
			//头部删除点击函数(可多删)
			delMany() {
				let params = [];
				let i = 0;
				this.chooseAll.forEach((d,i)=>{
					params.push(d.id);
				})
				this.delByIds(params);
			},
			//品牌选择取消按钮
			cancelBrand() {
				this.chooseBrand = false;
			},
			//品牌选择提交按钮
			submitBrand() {
				this.chooseBrand = false;
				axios.get("/goodsType/chooseBrand?goodsTypeId=" + this.typeId + "&brandIds=" + this.valBrand).then(resp => {
					if (resp.code == 20000) {
						this.$message.success(resp.msg);
					} else {
						this.$message.error(resp.msg);
					}
				})
			},
			//新增/修改表单提交
			submitForm() {
				let params = this.dataForm;
				if (params.id == null || params.id == '' || params.id == undefined) {
					axios.post("/goodsType", params).then(resp => {
						if (resp.code == 20000) {
							this.$message.success(resp.msg);
							this.queryAll();
							this.getTree();
						} else {
							this.$message.error(resp.msg);
						}
					})
				} else {
					axios.put("/goodsType", params).then(resp => {
						if (resp.code == 20000) {
							this.$message.success(resp.msg);
							this.queryAll();
							this.getTree();
						} else {
							this.$message.error(resp.msg);
						}
					})
				}
				this.open = false;
			},
			//新增/修改表单取消
			cancel() {
				this.open = false;
				this.resetDataForm();
			},
			//新增点击函数
			addFrom() {
				this.title = "新增商品类型";
				this.open = true;
				this.resetDataForm();
			},
			resetDataForm(){
				this.dataForm = {
					pid:this.queryParams.pid,
					typeName: '',
					brandTypeDescription: '',
					id: '',
				};
			},
			//表单选用品牌点击函数
			handleChoose(idx, row) {
				this.chooseBrand = true;
				this.typeId = row.id;
				this.dataBrand=[];
				this.valBrand=[];
				axios.get("/goodsType/" + row.id).then(resp => {
					this.dataBrand = resp.data.dataBrand;
					resp.data.valBrand.forEach(d=>{
						this.valBrand.push(d.id);
					})
				})
			},
			//表单修改点击函数
			handleEdit(idx, row) {
				this.dataForm = JSON.parse(JSON.stringify(row));
				this.title = "修改商品类型";
				this.open = true;
				axios.get("/goodsType/getAll").then(resp => {
					console.log(resp);
					if (resp.code == 20000) {
						this.allList = resp.data;
					}
				});
			},
			//表单删除点击函数
			handleDelete(idx, row) {
				this.delByIds(row.id);
			},
			//树形组件点击事件
			handleNodeClick(val) {
				this.queryParams.pid = val.id;
				this.queryAll();
			},
			//重置按钮
			reset() {
				this.queryParams.typeName = '';
				this.queryParams.pid = '10';
				this.queryAll();
				this.getTree();
			},
			//查询函数
			quarryList() {
				this.queryParams.pid = null;
				this.queryAll();
				// this.getTree();
			},
			//页码改变
			handleCurrentChange(val) {
				this.queryParams.pageNum = val;
				this.queryAll();
			},
			//每页数量改变
			handleSizeChange(val) {
				this.queryParams.pageSize = val;
				this.queryAll();
			},
			//表单选择器选择函数(参数为当前选择的列)
			handleSelectionChange(val) {
				this.chooseAll = val;
				if (this.chooseAll.length > 0) {
					this.delClick = false;
				} else {
					this.delClick = true;
				}
			},
			//s设置表头行的样式
			tableHeaderColor({
				row,
				column,
				rowIndex,
				columnIndex
			}) {
				return 'background-color:#dcdcdc;color:#6b6b6b;font-wight:100;font-size:15px;text-align:center'
			},
			//树形数据请求
			getTree() {
				axios.get("/goodsType/getTree").then(resp => {
					this.dataTree = resp.data;
				});
			},
			queryAll() {
				axios.get("/goodsType", {
					params: this.queryParams
				}).then(resp => {
					if (resp.code == 20000) {
						this.dataList = resp.data.list;
						this.queryParams.pageNum = resp.data.pageNum;
						this.queryParams.pageSize = resp.data.pageSize;
						this.queryParams.total = resp.data.total;
					} else if (resp.code == 50002) {
						this.dataList = [];
						this.queryParams.pageNum = 1;
						this.queryParams.pageSize = 10;
						this.queryParams.total = 0;
					}
				})
			},
			//通过ids删除(一或多都可以)
			delByIds(ids) {
				this.$confirm(`此操作将永久删除编号为${ids}的数据, 是否继续?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$axios.delete("/goodsType?ids=" + ids).then(resp => {
						if (resp.code == 20000) {
							this.$message.success(`删除编号为${ids}的数据` + resp.msg);
							this.queryAll();
						} else {
							this.$message.error(`删除编号为${ids}的数据` + resp.msg);
						}
					});
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			}
		},
		mounted() {
			this.getTree();
			this.queryParams.pid = "10";
			this.queryAll();
		},
	};
</script>

<style>
</style>
